<?php include "checkSessionForAuthen.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Tài Khoản Người Dùng</title>
        <?php include("import.php"); ?>
    </head>

    <body>
        <!--header-->
        <?php
        include("../service/userService.php");
        include "header.php";
        ?>
        <!--end header-->
        <!-- Container -->
        <?php
        /* process paging event */
        if (isset($page_num)) {
            $page_num = $_GET['page_num'];
        } else {
            $page_num = 1;
        }

        $row_per_page = 10;

        if (isset($keyword)) {
            $keyword = $_GET['keyword'];
        } else {
            $keyword = "";
        }

        $total = countNumOfUserByName($keyword);
        $total_page = ceil($total / $row_per_page);
        $start_position = ($page_num * $row_per_page) - ($row_per_page);
        ?>
        <form id="form" action="user.php" method="get">
            <!-- Container -->
            <div id="container">
                <div class="shell">
                    <!-- Message OK -->
                    
                    <!-- End Message OK -->

                    <!-- Message Error -->
                    <div class="msg msg-error msg-hidden">
                        <p><strong>You must select a file to upload first!</strong></p>
                        <a href="#" class="close">close</a>
                    </div>
                    <!-- End Message Error -->

                    <br />
                    <!-- Main -->
                    <div id="main">
                        <div class="cl">&nbsp;</div>

                        <!-- Content -->
                        <div id="content">

                            <!-- Box -->
                            <div class="box">
                                <!-- Box Head -->
                                <div class="box-head">
                                    <h2 class="left">Danh Sách Tài Khoản Người Dùng</h2> 
                                    <div class="right">
                                        <label>Tìm Kiếm:</label>
                                        <input type="text" class="field small-field" name="keyword" />
                                    </div>
                                </div>
                                <!-- End Box Head -->	

                                <!-- Table -->
                                <div class="table" id="tblCategory">
                                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <th>STT</th>
                                            <th>Tên Đăng Nhập</th>
                                        </tr>
                                        <?php
                                        $count = 1;
                                        $result = getUserBy($keyword, $start_position, $row_per_page);

                                        while ($row = ConnectionManager::getInstance()->fetchArray()) {
                                            ?>
                                            <tr>
                                                <td><?php echo $count; ?></td>
                                                <td>
                                                    <h3><?php echo $row["USER_USERNAME"]; ?></h3>
                                                </td>
                                            </tr>
                                            <?php
                                            $count++;
                                        }
                                        ?>
                                    </table>
                                    <!-- Pagging -->
                                    <div class="pagging">
                                        <div class="left">Trang <?php echo(" " . $page_num . "  trong tổng số " . $total_page . " trang "); ?></div>
                                        <div class="right">
                                            <?php
                                            for ($i = 1; $i <= $total_page; $i++) {
                                                echo("<a href='" . "user.php?" . "&keyword=" . $keyword . "&page_num=" . $i . "'>" . $i . "</a>");
                                            }
                                            ?>
                                        </div>
                                    </div>
                                    <!-- End Pagging -->
                                </div>
                                <!-- Table -->
                            </div>
                            <!-- End Box -->

                            <!-- Box -->
                            <div class="box">
                                <!-- Box Head -->
                                <div class="box-head">
                                    <h2>Thao tác</h2>
                                </div>
                                <!-- End ofBox Head -->
                                <div class="box-content">
                                    <div class="buttons">
                                        <input type="button" class="button" value="Thêm Tài Khoản" onclick="window.location = 'addUser.php';"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- End Content -->
                        <div class="cl">&nbsp;</div>			
                    </div>
                    <!-- Main -->
                </div>
            </div>
            <!-- End Container -->
        </form>
        <!-- End Container -->

        <!--footer-->
        <?php include "footer.php"; ?>
        <!--end footer-->
        <?php ConnectionManager::getInstance()->closeConnection(); ?>
    </body>
</html>